home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / fstwrt4.zip / FASTWR.ASM < prev    next >
Assembly Source File  |  1993-01-04  |  19KB  |  439 lines

  1.  
  2. ;       FASTWR.ASM
  3. ;       Fast screen writing routines
  4. ;       By Brian Foley
  5. ;       Last update: 11/08/87
  6.  
  7.  
  8. ;****************************************************** Equates
  9.  
  10.         Mono    = 0
  11.         CGA     = 1
  12.         EGA     = 2
  13.         MCGA    = 3
  14.         VGA     = 4
  15.  
  16. ;****************************************************** Data
  17.  
  18. DATA    SEGMENT BYTE PUBLIC
  19.  
  20.         EXTRN   BaseOfScreen : WORD     ;Pascal variables
  21.         EXTRN   WaitForRetrace : BYTE
  22.  
  23.         CurrentMode     DB      ?       ;local variables
  24.         Display         DB      ?
  25.  
  26. DATA    ENDS
  27.  
  28. ;****************************************************** Code
  29.  
  30. CODE    SEGMENT BYTE PUBLIC
  31.  
  32.         ASSUME  CS:CODE,DS:DATA
  33.  
  34.         PUBLIC  FastWrite, FastWriteNA, ChangeAttribute, MoveFromScreen
  35.         PUBLIC  MoveToScreen, CurrentDisplay, CurrentVideoMode
  36.  
  37. ;****************************************************** CalcOffset
  38.  
  39. ;Calculates offset in video memory corresponding to Row,Column
  40. ;On entry, CH has Row, BX has Column (both 1-based)
  41. ;On exit, ES:DI points to proper address in video memory, AX = 0
  42.  
  43. CalcOffset      PROC NEAR
  44.  
  45.         XOR     AX,AX                   ;AX = 0
  46.         MOV     CL,AL                   ;CL = 0
  47.         MOV     BH,AL                   ;BH = 0
  48.         DEC     CH                      ;Row (in CH) to 0..24 range
  49.         SHR     CX,1                    ;CX = Row * 128
  50.         MOV     DI,CX                   ;Store in DI
  51.         SHR     DI,1                    ;DI = Row * 64
  52.         SHR     DI,1                    ;DI = Row * 32
  53.         ADD     DI,CX                   ;DI = (Row * 160)
  54.         DEC     BX                      ;Col (in BX) to 0..79 range
  55.         SHL     BX,1                    ;Account for attribute bytes
  56.         ADD     DI,BX                   ;DI = (Row * 160) + (Col * 2)
  57.         MOV     ES,BaseOfScreen         ;ES:DI points to BaseOfScreen:Row,Col
  58.         RET                             ;Return
  59.  
  60. CalcOffset      ENDP
  61.  
  62. ;****************************************************** FastWrite
  63.  
  64. ;procedure FastWrite(St : String; Row, Col, Attr : Byte);
  65. ;Write St at Row,Col in Attr (video attribute) without snow
  66.  
  67. ;equates for parameters:
  68. FWAttr          EQU     BYTE PTR [BP+6]
  69. FWCol           EQU     BYTE PTR [BP+8]
  70. FWRow           EQU     BYTE PTR [BP+10]
  71. FWSt            EQU     DWORD PTR [BP+12]
  72.  
  73. FastWrite       PROC FAR
  74.  
  75.         PUSH    BP                      ;Save BP
  76.         MOV     BP,SP                   ;Set up stack frame
  77.         PUSH    DS                      ;Save DS
  78.         MOV     CH,FWRow                ;CH = Row
  79.         MOV     BL,FWCol                ;BL = Column
  80.         CALL    CalcOffset              ;Call routine to calculate offset
  81.         MOV     CL,WaitForRetrace       ;Grab this before changing DS
  82.         LDS     SI,FWSt                 ;DS:SI points to St[0]
  83.         CLD                             ;Set direction to forward
  84.         LODSB                           ;AX = Length(St); DS:SI -> St[1]
  85.         XCHG    AX,CX                   ;CX = Length; AL = WaitForRetrace
  86.         JCXZ    FWExit                  ;If string empty, exit
  87.         MOV     AH,FWAttr               ;AH = Attribute
  88.         RCR     AL,1                    ;If WaitForRetrace is False...
  89.         JNC     FWMono                  ; use "FWMono" routine
  90.         MOV     DX,03DAh                ;Point DX to CGA status port
  91. FWGetNext:
  92.         LODSB                           ;Load next character into AL
  93.                                         ; AH already has Attr
  94.         MOV     BX,AX                   ;Store video word in BX
  95.         CLI                             ;No interrupts now
  96. FWWaitNoH:
  97.         IN      AL,DX                   ;Get 6845 status
  98.         TEST    AL,8                    ;Vertical retrace in progress?
  99.         JNZ     FWStore                 ;If so, go
  100.         RCR     AL,1                    ;Else, wait for end of
  101.         JC      FWWaitNoH               ; horizontal retrace
  102. FWWaitH:
  103.         IN      AL,DX                   ;Get 6845 status again
  104.         RCR     AL,1                    ;Wait for horizontal
  105.         JNC     FWWaitH                 ; retrace
  106. FWStore:
  107.         MOV     AX,BX                   ;Move word back to AX...
  108.         STOSW                           ; and then to screen
  109.         STI                             ;Allow interrupts!
  110.         LOOP    FWGetNext               ;Get next character
  111.         JMP     FWExit                  ;Done
  112. FWMono:
  113.         LODSB                           ;Load next character into AL
  114.                                         ; AH already has Attr
  115.         STOSW                           ;Move video word into place
  116.         LOOP    FWMono                  ;Get next character
  117. FWExit:
  118.         POP     DS                      ;Restore DS
  119.         MOV     SP,BP                   ;Restore SP
  120.         POP     BP                      ;Restore BP
  121.         RET     10                      ;Remove parameters and return
  122.  
  123. FastWrite       ENDP
  124.  
  125. ;****************************************************** FastWriteNA
  126.  
  127. ;procedure FastWriteNA(St : String; Row, Col : Byte);
  128. ;Write St at Row,Col without snow, and don't change video attributes
  129.  
  130. ;equates for parameters:
  131. FNCol           EQU     BYTE PTR [BP+6]
  132. FNRow           EQU     BYTE PTR [BP+8]
  133. FNSt            EQU     DWORD PTR [BP+10]
  134.  
  135. FastWriteNA     PROC FAR
  136.  
  137.         PUSH    BP                      ;Save BP
  138.         MOV     BP,SP                   ;Set up stack frame
  139.         PUSH    DS                      ;Save DS
  140.         MOV     CH,FNRow                ;CH = Row
  141.         MOV     BL,FNCol                ;BL = Column
  142.         CALL    CalcOffset              ;Call routine to calculate offset
  143.         MOV     CL,WaitForRetrace       ;Grab this before changing DS
  144.         LDS     SI,FNSt                 ;DS:SI points to St[0]
  145.         CLD                             ;Set direction to forward
  146.         LODSB                           ;AX = Length(St); DS:SI -> St[1]
  147.         XCHG    AX,CX                   ;CX = Length; AL = Wait
  148.         JCXZ    FNExit                  ;If string empty, exit
  149.         RCR     AL,1                    ;If WaitForRetrace is False...
  150.         JNC     FNNoWait                ; use FNNoWait routine
  151.         MOV     DX,03DAh                ;Point DX to CGA status port
  152. FNGetNext:
  153.         LODSB                           ;Load next character into AL
  154.         MOV     AH,AL                   ;Store char in AH
  155.         CLI                             ;No interrupts now
  156. FNWaitNoH:
  157.         IN      AL,DX                   ;Get 6845 status
  158.         TEST    AL,8                    ;Check for vertical retrace
  159.         JNZ     FNStore                 ; In progress? go
  160.         RCR     AL,1                    ;Else, wait for end of
  161.         JC      FNWaitNoH               ; horizontal retrace
  162. FNWaitH:
  163.         IN      AL,DX                   ;Get 6845 status again
  164.         RCR     AL,1                    ;Wait for horizontal
  165.         JNC     FNWaitH                 ; retrace
  166. FNStore:
  167.         MOV     AL,AH                   ;Move char back to AL...
  168.         STOSB                           ; and then to screen
  169.         STI                             ;Allow interrupts
  170.         INC     DI                      ;Skip attribute bytes
  171.         LOOP    FNGetNext               ;Get next character
  172.         JMP     FNExit                  ;Done
  173. FNNoWait:
  174.         MOVSB                           ;Move character to screen
  175.         INC     DI                      ;Skip attribute bytes
  176.         LOOP    FNNoWait                ;Get next character
  177. FNExit:
  178.         POP     DS                      ;Restore DS
  179.         MOV     SP,BP                   ;Restore SP
  180.         POP     BP                      ;Restore BP
  181.         RET     8                       ;Remove parameters and return
  182.  
  183. FastWriteNA     ENDP
  184.  
  185. ;****************************************************** ChangeAttribute
  186.  
  187. ;procedure ChangeAttribute(Number : Word; Row, Col, Attr : Byte);
  188. ;Change Number video attributes to Attr starting at Row,Col
  189.  
  190. ;equates for parameters:
  191. CAAttr          EQU     BYTE PTR [BP+6]
  192. CACol           EQU     BYTE PTR [BP+8]
  193. CARow           EQU     BYTE PTR [BP+10]
  194. CANumber        EQU     WORD PTR [BP+12]
  195.  
  196. ChangeAttribute PROC FAR
  197.  
  198.         PUSH    BP                      ;Save BP
  199.         MOV     BP,SP                   ;Set up stack frame
  200.         MOV     CH,CARow                ;CH = Row
  201.         MOV     BL,CACol                ;BL = Column
  202.         CALL    CalcOffset              ;Call routine to calculate offset
  203.         INC     DI                      ;Skip character
  204.         CLD                             ;Set direction to forward
  205.         MOV     CX,CANumber             ;CX = Number to change
  206.         JCXZ    CAExit                  ;If zero, exit
  207.         MOV     AL,CAAttr               ;AL = Attribute
  208.         CMP     WaitForRetrace,1        ;Get wait state
  209.         JNE     CANoWait                ;If WaitForRetrace is False
  210.                                         ; use CANoWait routine
  211.         MOV     AH,AL                   ;Store attribute in AH
  212.         MOV     DX,03DAh                ;Point DX to CGA status port
  213. CAGetNext:
  214.         CLI                             ;No interrupts now
  215. CAWaitNoH:
  216.         IN      AL,DX                   ;Get 6845 status
  217.         TEST    AL,8                    ;Check for vert. retrace
  218.         JNZ     CAGo                    ;In progress? Go
  219.         RCR     AL,1                    ;Wait for end of horizontal
  220.         JC      CAWaitNoH               ; retrace
  221. CAWaitH:
  222.         IN      AL,DX                   ;Get 6845 status again
  223.         RCR     AL,1                    ;Wait for horizontal
  224.         JNC     CAWaitH                 ; retrace
  225. CAGo:
  226.         MOV     AL,AH                   ;Move Attr back to AL...
  227.         STOSB                           ; and then to screen
  228.         STI                             ;Allow interrupts
  229.         INC     DI                      ;Skip characters
  230.         LOOP    CAGetNext               ;Look for next opportunity
  231.         JMP     CAExit                  ;Done
  232. CANoWait:
  233.         STOSB                           ;Change the attribute
  234.         INC     DI                      ;Skip characters
  235.         LOOP    CANoWait                ;Get next character
  236. CAExit:                                 ;Next instruction
  237.         MOV     SP,BP                   ;Restore SP
  238.         POP     BP                      ;Restore BP
  239.         RET     8                       ;Remove parameters and return
  240.  
  241. ChangeAttribute ENDP
  242.  
  243. ;****************************************************** MoveFromScreen
  244.  
  245. ;procedure MoveFromScreen(var Source, Dest; Length : Word);
  246. ;Move Length words from Source (video memory) to Dest without snow
  247.  
  248. ;equates for parameters:
  249. MFLength        EQU     WORD PTR [BP+6]
  250. MFDest          EQU     DWORD PTR [BP+8]
  251. MFSource        EQU     DWORD PTR [BP+12]
  252.  
  253. MoveFromScreen  PROC FAR
  254.  
  255.         PUSH    BP                      ;Save BP
  256.         MOV     BP,SP                   ;Set up stack frame
  257.         MOV     BX,DS                   ;Save DS in BX
  258.         MOV     AL,WaitForRetrace       ;Grab before changing DS
  259.         LES     DI,MFDest               ;ES:DI points to Dest
  260.         LDS     SI,MFSource             ;DS:SI points to Source
  261.         MOV     CX,MFLength             ;CX = Length
  262.         CLD                             ;Set direction to forward
  263.         RCR     AL,1                    ;Check WaitForRetrace
  264.         JNC     MFNoWait                ;False? Use MFNoWait routine
  265.         MOV     DX,03DAh                ;Point DX to CGA status port
  266. MFNext:
  267.         CLI                             ;No interrupts now
  268. MFWaitNoH:
  269.         IN      AL,DX                   ;Get 6845 status
  270.         TEST    AL,8                    ;Check for vertical retrace
  271.         JNZ     MFGo                    ;In progress? go
  272.         RCR     AL,1                    ;Wait for end of horizontal
  273.         JC      MFWaitNoH               ; retrace
  274. MFWaitH:
  275.         IN      AL,DX                   ;Get 6845 status again
  276.         RCR     AL,1                    ;Wait for horizontal
  277.         JNC     MFWaitH                 ; retrace
  278. MFGo:
  279.         LODSW                           ;Load next video word into AX
  280.         STI                             ;Allow interrupts
  281.         STOSW                           ;Store video word in Dest
  282.         LOOP    MFNext                  ;Get next video word
  283.         JMP     MFExit                  ;All Done
  284. MFNoWait:
  285.         REP     MOVSW                   ;That's it!
  286. MFExit:
  287.         MOV     DS,BX                   ;Restore DS
  288.         MOV     SP,BP                   ;Restore SP
  289.         POP     BP                      ;Restore BP
  290.         RET     10                      ;Remove parameters and return
  291.  
  292. MoveFromScreen  ENDP
  293.  
  294. ;****************************************************** MoveToScreen
  295.  
  296. ;procedure MoveToScreen(var Source, Dest; Length : Word);
  297. ;Move Length words from Source to Dest (video memory) without snow
  298.  
  299. ;equates for parameters:
  300. MTLength        EQU     WORD PTR [BP+6]
  301. MTDest          EQU     DWORD PTR [BP+8]
  302. MTSource        EQU     DWORD PTR [BP+12]
  303.  
  304. MoveToScreen    PROC FAR
  305.  
  306.         PUSH    BP                      ;Save BP
  307.         MOV     BP,SP                   ;Set up stack frame
  308.         PUSH    DS                      ;Save DS
  309.         MOV     AL,WaitForRetrace       ;Grab before changing DS
  310.         LES     DI,MTDest               ;ES:DI points to Dest
  311.         LDS     SI,MTSource             ;DS:SI points to Source
  312.         MOV     CX,MTLength             ;CX = Length
  313.         CLD                             ;Set direction to forward
  314.         RCR     AL,1                    ;Check WaitForRetrace
  315.         JNC     MTNoWait                ;False? Use MTNoWait routine
  316.         MOV     DX,03DAh                ;Point DX to CGA status port
  317. MTGetNext:
  318.         LODSW                           ;Load next video word into AX
  319.         MOV     BX,AX                   ;Store video word in BX
  320.         CLI                             ;No interrupts now
  321. MTWaitNoH:
  322.         IN      AL,DX                   ;Get 6845 status
  323.         TEST    AL,8                    ;Check for vertical retrace
  324.         JNZ     MTGo                    ;In progress? Go
  325.         RCR     AL,1                    ;Wait for end of horizontal
  326.         JC      MTWaitNoH               ; retrace
  327. MTWaitH:
  328.         IN      AL,DX                   ;Get 6845 status again
  329.         RCR     AL,1                    ;Wait for horizontal
  330.         JNC     MTWaitH                 ; retrace
  331. MTGo:
  332.         MOV     AX,BX                   ;Move word back to AX...
  333.         STOSW                           ; and then to screen
  334.         STI                             ;Allow interrupts
  335.         LOOP    MTGetNext               ;Get next video word
  336.         JMP     MTExit                  ;All done
  337. MTNoWait:
  338.         REP     MOVSW                   ;That's all!
  339. MTExit:
  340.         POP     DS                      ;Restore DS
  341.         MOV     SP,BP                   ;Restore SP
  342.         POP     BP                      ;Restore BP
  343.         RET     10                      ;Remove parameters and return
  344.  
  345. MoveToScreen    ENDP
  346.  
  347. ;****************************************************** CurrentModePrim
  348.  
  349. ;Returns current video mode in AL
  350.  
  351. CurrentModePrim PROC NEAR
  352.  
  353.         MOV     AH,0Fh                  ;Get video mode function
  354.         INT     10h                     ;Call BIOS
  355.         MOV     CurrentMode,AL          ;Save it
  356.         RET                             ;Return
  357.  
  358. CurrentModePrim ENDP
  359.  
  360. ;****************************************************** CurrentDisplay
  361.  
  362. ;function CurrentDisplay : DisplayType;
  363. ;Returns type of the currently active display.
  364.  
  365. JunkValue       = 0FFFFh
  366.  
  367. CurrentDisplay  PROC FAR
  368.  
  369.         ;get the current video mode
  370.         CALL     CurrentModePrim        ;Call primitive routine
  371.  
  372.         ;test for VGA
  373.         MOV     Display,VGA             ;Assume VGA
  374.         MOV     CX,JunkValue            ;Load CX with junk value
  375.         MOV     AX,1C00h                ;Save/Restore video state
  376.         INT     10h
  377.         CMP     AL,1Ch                  ;AL = $1C signals valid call
  378.         JE      CDexit                  ;Adapter type known
  379.  
  380.         ;test for MCGA
  381.         MOV     Display,MCGA            ;Assume MCGA
  382.         MOV     BL,32h                  ;Choose Enable
  383.         MOV     AX,1200h                ;Enable/Disable video addressing
  384.         INT     10h
  385.         CMP     AL,12h                  ;AL = $12 signals valid call
  386.         JE      CDexit                  ;Adapter type known
  387.  
  388.         ;test for EGA
  389.         MOV     Display,EGA             ;Assume EGA
  390.         MOV     BX,0FF10h               ;Return EGA information
  391.         MOV     CX,JunkValue            ;Load CX with junk value
  392.         MOV     AX,1200h                ;Alternate function select
  393.         INT     10h
  394.         XOR     AL,AL                   ;AL = 0
  395.         CMP     CX,JunkValue            ;CX unchanged?
  396.         JE      CDplain                 ;If so, not an EGA
  397.         CMP     BH,1                    ;BH should be 0 or 1
  398.         JA      CDplain                 ;Mono or CGA if it isn't
  399.  
  400.         ;See if EGA is the active display
  401.         CMP     BH,1                    ;mono display?
  402.         JE      CDegaMono               ;If so, use mono check
  403.         CMP     CurrentMode,7           ;In mono mode?
  404.         JE      CDplain                 ;If so, we're not on the EGA
  405.         JMP     SHORT CDexit            ;else, it's an EGA
  406.  
  407. CDegaMono:
  408.         CMP     CurrentMode,7           ;Current mode = 7?
  409.         JNE     CDplain                 ;Exit if not
  410.  
  411. CDexit:
  412.         MOV     AL,Display              ;set return value
  413.         RET                             ;Return
  414.  
  415. CDplain:
  416.         MOV     Display,CGA             ;Assume CGA
  417.         CMP     CurrentMode,7           ;Mono mode
  418.         JNE     CDexit                  ;Done if not
  419.         MOV     Display,Mono            ;Else, Mono
  420.         JMP     SHORT CDexit            ;Done
  421.  
  422. CurrentDisplay  ENDP
  423.  
  424. ;****************************************************** CurrentVideoMode
  425.  
  426. ;function CurrentVideoMode : Byte;
  427. ;Returns current video mode in AL
  428.  
  429. CurrentVideoMode        PROC FAR
  430.  
  431.         CALL     CurrentModePrim        ;Call primitive routine
  432.         RET                             ;Return
  433.  
  434. CurrentVideoMode        ENDP
  435.  
  436. CODE    ENDS
  437.  
  438.         END
  439.